package com.oceansoft.core.common.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * URL处理
 * @author Administrator
 *
 */
public final class UrlUtil {
	public static final String FILE_URL_PREFIX = "file:";

	public static URL getURL(String resourceLocation) throws RuntimeException {
		return getURL(resourceLocation, null);
	}

	public static URL getURL(String resourceLocation, ClassLoader loader) throws RuntimeException {
		if (resourceLocation.startsWith("file:")) {
			resourceLocation = resourceLocation.substring("file:".length());
		}
		File file = new File(resourceLocation);
		if (file.exists());
		URL url;
		try {
			url = new URL(resourceLocation);
		} catch (MalformedURLException e) {
			try {
				url = new URL("file:" + resourceLocation);
			} catch (MalformedURLException ex2) {
				throw new RuntimeException("The [resourceLocation=" + resourceLocation
						+ "] is neither a URL not a well-formed file path.", ex2);
			}
			url = null;
			if (loader != null) {
				url = loader.getResource(resourceLocation);
			}
			if (url == null) {
				url = Thread.currentThread().getContextClassLoader().getResource(resourceLocation);
				if (url == null) {
					url = UrlUtil.class.getClassLoader().getResource(resourceLocation);
					if (url == null)
						throw new RuntimeException("Class path resource [" + resourceLocation
								+ "] cannot be resolved to URL because it does not exist.");
				}
			}
		}
		return url;
	}
}
